package com.example.community.config;

import com.example.community.quartz.AlphaJob;
import com.example.community.quartz.PostScoreRefreshJob;
import com.example.community.quartz.WKImageDeleteJob;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

/**
 * @Author: huang
 * @Date: 2021/10/9 14:48
 * @Description:
 */
@Configuration
public class QuartzConfig {

    //FactoryBean作用：简化bean的实例化过程
    //1.通过FactoryBean封装Bean的实例化过程
    //2.将FactoryBean装配到spring容器中
    //3.将FactoryBean注入给其他的bean
    //4.其他的bean即可获得FactoryBean所管理的对象实例

    /**
     * @return 配置jobDetail
     */
    //@Bean
    public JobDetailFactoryBean alphaJobDetail() {
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        //声明要配置的job类
        factoryBean.setJobClass(AlphaJob.class);
        //声明job的名称
        factoryBean.setName("alphaJob");
        //声明job的分组
        factoryBean.setGroup("alphaJobGroup");
        //job是否长久的存在
        factoryBean.setDurability(true);
        //job是否可被恢复
        factoryBean.setRequestsRecovery(true);
        return factoryBean;
    }

    /**
     *
     * @param alphaJobDetail
     * @return      配置trigger
     *          SimpleTriggerFactoryBean：简单的定时
     *          CronTriggerFactoryBean：可实现复杂的定时，如每年的1号进行清除
     */
    //@Bean
    public SimpleTriggerFactoryBean alphaTrigger(JobDetail alphaJobDetail) {
        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        //确定JobDetail
        factoryBean.setJobDetail(alphaJobDetail);
        //Trigger的名称
        factoryBean.setName("alphaTrigger");
        //Trigger的分组
        factoryBean.setGroup("alphaTriggerGroup");
        //时间间隔
        factoryBean.setRepeatInterval(3000);
        //保存数据的类型
        factoryBean.setJobDataMap(new JobDataMap());

        return factoryBean;
    }


    // 刷新帖子分数任务
    @Bean
    public JobDetailFactoryBean postScoreRefreshJobDetail() {
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        factoryBean.setJobClass(PostScoreRefreshJob.class);
        factoryBean.setName("postScoreRefreshJob");
        factoryBean.setGroup("communityJobGroup");
        factoryBean.setDurability(true);
        factoryBean.setRequestsRecovery(true);
        return factoryBean;
    }
    // 配置Trigger(SimpleTriggerFactoryBean, CronTriggerFactoryBean)
    @Bean
    public SimpleTriggerFactoryBean postScoreRefreshTrigger(JobDetail postScoreRefreshJobDetail) {
        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        factoryBean.setJobDetail(postScoreRefreshJobDetail);
        factoryBean.setName("postScoreRefreshTrigger");
        factoryBean.setGroup("communityTriggerGroup");
        factoryBean.setRepeatInterval(1000 * 60 * 3);
        factoryBean.setJobDataMap(new JobDataMap());
        return factoryBean;
    }

    // 删除图片任务
    @Bean
    public JobDetailFactoryBean wkImageDeleteJobDetail() {
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        factoryBean.setJobClass(WKImageDeleteJob.class);
        factoryBean.setName("wkImageDeleteJob");
        factoryBean.setGroup("communityJobGroup");
        factoryBean.setDurability(true);
        factoryBean.setRequestsRecovery(true);
        return factoryBean;
    }
    // 配置Trigger(SimpleTriggerFactoryBean, CronTriggerFactoryBean)
    @Bean
    public SimpleTriggerFactoryBean wkImageDeleteTrigger(JobDetail wkImageDeleteJobDetail) {
        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        factoryBean.setJobDetail(wkImageDeleteJobDetail);
        factoryBean.setName("wkImageDeleteJobDetail");
        factoryBean.setGroup("communityTriggerGroup");
        factoryBean.setRepeatInterval(1000 * 60 * 3);
        factoryBean.setJobDataMap(new JobDataMap());
        return factoryBean;
    }

}
